import {
  crmMessagNumAPI
} from '@/api/crm/message'
import { queryBaseUserList, depListAPI } from "@/api/common";
/**
 * 消息记录
 */
const app = {
  state: {
    // 待办事项消息
    messageNum: {
      todayCustomer: 0,
      followLeads: 0,
      followCustomer: 0,
      checkContract: 0,
      checkReceivables: 0,
      remindReceivablesPlan: 0,
      endContract: 0,
      totalNum: 0
    },
    userAllList: [], // 人员列表
    depAllList: [], // 部门列表
    depAllTreeList: [], // 部门树列表
    allCount: 0,
    leftCount: [0, 0, 0, 0, 0, 0, 0, 0],
    showDetail: false,
    detailThis: {}, // 预存this
  },

  mutations: {
    /**
     * 更改待办事项
     */
    SET_MESSAGENUM: (state, messageNum) => {
      let totalNum = 0
      for (const key in messageNum) {
        if (key != 'totalNum' && ['checkContract', 'checkReceivables', 'refundnum'].includes(key)) {
          totalNum += (messageNum[key] || 0)
        }
      }
      messageNum.totalNum = totalNum
      state.messageNum = messageNum
    },
    SET_UserAllList(state, data) {
      state.userAllList = data
    },
    SET_DepAllList(state, data) {
      state.depAllList = data;
    },
    SET_DepALLTreeList(state, data) {
      state.depAllTreeList = data
    },
    SET_MSGiNFO(state, data) {
      state.allCount = data.allCount
      state.leftCount = data.leftCount
    },
    SET_showDetail(state, data) {
      state.showDetail = data
    },
    SET_detailThis(state, data) {
      state.detailThis = data;
    }
  },

  actions: {
    // 登录
    GetMessageNum({
      state,
      commit
    }) {
      return new Promise((resolve, reject) => {
        crmMessagNumAPI()
          .then(response => {
            commit('SET_MESSAGENUM', response.data)
            commit('SET_CRMROUTERSNUM', state.messageNum.totalNum)
            resolve(response)
          })
          .catch(error => {
            reject(error)
          })
      })
    },
    async initDep({ commit }) {
      const resUser = await queryBaseUserList({ pageType: 0 });
      const resDep = await depListAPI();
      const resDepTree = await depListAPI({ type: "tree" });
      commit('SET_UserAllList', resUser.data.list);
      commit('SET_DepAllList', resDep.data);
      commit("SET_DepALLTreeList", resDepTree.data);
    },
    // 更新人员列表
    async initUserList({ commit }) {
      const resUser = await queryBaseUserList({ pageType: 0 });
      commit('SET_UserAllList', resUser.data.list);
    },
    // 更新部门
    async initDepList({ commit }) {
      const resDep = await depListAPI();
      const resDepTree = await depListAPI({ type: "tree" });
      commit('SET_DepAllList', resDep.data);
      commit("SET_DepALLTreeList", resDepTree.data);
    }
  }
}

export default app
